import {
  defineStore
} from 'pinia'
export const addressUserStore = defineStore('address', {
  state: () => {
    return {
      addrestag: {
        list: []
      },
      address: {
        list: []
      },
      //定义选中的地址
      selectaddress:{
        address:""
      }
    }
  },
  actions: {
    //发起请求获取标签
    async getaddrestag() {
      const response = await uni.$http.get("/addres/getaddrestag")
      this.addrestag.list = response.data
    },
    //发起请求添加标签
    async addaddrestag(value) {
      const response = await uni.$http.post("/addres/addaddrestag", {
        "value": value
      })
      this.getaddrestag()
      uni.showToast({
        title: response.data,
        duration: 2000,
        icon: "success"
      });
    },
    //发起请求删除标签
    async deleteaddrestag(id) {
      const response = await uni.$http.delete("/addres/deleteaddrestag", {
        "id": id
      })
        this.getaddrestag()
      uni.showToast({
        title: response.data,
        duration: 2000,
        icon: "success"
      });
    },
    //获取要订单的地址
    async getaddre(data){
      const response = await uni.$http.get("/addres/getaddr",{'id':data.id})
       this.selectaddress.address = response.data
       uni.navigateTo({
         url: `/subPack/submitorder/submitorder?goods_id=${data.goods_id}&goodcount=${data.goodcount}`
       })
      },
    //获取默认选择地址
    async getdeladdre(){
      const response = await uni.$http.get("/addres/getdeladdre")
       this.selectaddress.address = response.data
      },
    //添加收货地址
    async addaddress(data) {
      const response = await uni.$http.post("/addres/addaddress", data)
      if (response.data.code === 300) {
        uni.showToast({
          title: response.data.msg,
          duration: 2000,
          icon: "error"
        });
      } else {
       this.getaddrestag()
       uni.showModal({
         title: "提示",
         content: response.data.msg,
         showCancel: false,
         success: (res) => {
           if (res.confirm) {
             this.getaddrestag()
             uni.navigateTo({
               url: "/subPack/Address/Address"
             })
           }
         }
       });
      }
    },
    //获取收货地址
    async getaddress() {
      const response = await uni.$http.get("/addres/getaddress")
      this.address.list = response.data
    },
    //更改收货地址
    async updateaddress(data) {
      const response = await uni.$http.put("/addres/updateaddress", data)
      if (response.data.code === 300) {
        uni.showToast({
          title: response.data.msg,
          duration: 2000,
          icon: "error"
        });
      } else {
        this.getaddrestag()
        uni.showModal({
          title: "提示",
          content: response.data.msg,
          showCancel: false,
          success: (res) => {
            if (res.confirm) {
              this.getaddrestag()
              uni.navigateTo({
                url: "/subPack/Address/Address"
              })
            }
          }
        });

      }
    },
    //删除收货地址
    async deleteaddress(id) {
      const response = await uni.$http.delete("/addres/deleteaddress", {
        "id": id
      })
      this.getaddress()
      uni.showToast({
        title: response.data,
        duration: 2000,
        icon: "success"
      });
    },
  }
})